package service.community;

import com.mysql.jdbc.Connection;
import dao.BaseDao;
import dao.community.CommunityDao;
import dao.community.CommunityDaoImpl;
import domain.Community;
import domain.Page;
import util.DButils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class CommunityServiceImpl implements CommunityService {

    private CommunityDao communityDao;

    public CommunityServiceImpl() {
        communityDao = new CommunityDaoImpl();
    }

    /**
     * 查询所有的社区信息，并显示
     *
     * @param community
     * @return java.util.List<domain.Community>
     * @author 荣燊
     * @date 2020/7/13 14:50
     */
    public List<Community> getCommunityList(Community community, Page page) {
        Connection connection = null;
        List<Community> communityList = null;
        System.out.println("开始查询所有社区信息。。。。。。");
        try {
            connection = (Connection) BaseDao.getConnection();
            communityList = communityDao.getCommunityList(connection, page);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }

        return communityList;
    }



    @Override
    public List<Community> getCommunityList(Community community) {
        return null;
    }

    /**
     * 增加社区的服务信息
     *
     * @param community
     * @return boolean
     * @author 荣燊
     * @date 2020/7/14 11:07
     */
    @Override
    public boolean addCommunity(Community community) {
        boolean flag = false;
        Connection connection = null;
        try {
            connection = (Connection) BaseDao.getConnection();
            if (communityDao.addCommunity(connection, community) > 0)
                flag = true;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 根据community_id查询社区信息，并返回boolean类型的值
     *
     * @param community_id
     * @return
     */
    @Override
    public boolean getCommunityInfo(int community_id) {
        boolean flag = false;
        Connection connection = null;
        try {
            connection = (Connection) BaseDao.getConnection();
            if (communityDao.getCommunityList(connection, community_id) == true)
                flag = true;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 根据Community_id对象修改社区信息
     *
     * @param community_id
     * @return boolean
     * @author 荣燊
     * @date 2020/7/13 14:52
     */
    @Override
    public boolean modifyCommunity(int community_id, String commnity_info) {
        Connection connection = null;
        boolean flag = false;
        try {
            connection = (Connection) BaseDao.getConnection();
            if (communityDao.modifyCommunity(connection, community_id, commnity_info) > 0) {
                System.out.println("开始根据community对象更新社区信息。。。");
                flag = true;
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }

    /**
     * 根据community_id删除社区信息
     *
     * @param community_id
     * @return boolean
     * @author 荣燊
     * @date 2020/7/13 14:54
     */
    @Override
    public boolean deleteCommunity(int community_id) {
        Connection connection = null;
        boolean flag = false;
        try {
            connection = (Connection) BaseDao.getConnection();
            System.out.println("开始根据community_id删除社区信息。。。");
            if (communityDao.delteCommunity(connection, community_id) > 0)
                flag = true;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }

    /**
     * 获取社区记录条数
     *
     * @return int
     * @author 夏添
     * @date 2020/7/14 9:15
     */
    @Override
    public int getCont() {  //获取社区数量
        int count = communityDao.getCont();
        return  count;
    }
    /**
     * 条件筛选社区
     * @param  view_power
     * @return ArrayList
     * @author 夏天
     * @date 2020/7/21 20:32
     */
    @Override
    public ArrayList<Community> queryCommunity(String view_power){
        if(view_power.equals("all")){
            view_power = "";
            CommunityDao communityDao = new CommunityDaoImpl();
            try {
                List<Community> list = communityDao.getCommunityList();
                for (int i =0; i<list.size();i++){
                    view_power += list.get(i).getCommunity_id()+",";
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        ArrayList<Community> list =new ArrayList<>();
        if(view_power==null||view_power.equals("null"))
            return list;
        else {
            try {
                list = communityDao.queryCommunity(view_power);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return list;
    }
}
